# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua-pty
_pkgname=lpty
pkgver=1.2.2
_rockver=1
pkgrel=3
pkgdesc="Lua module to control other programs via PTYs"
url="https://www.tset.de/lpty/"
arch="all"
license="MIT"
source=$pkgname-$pkgver-1.tar.gz::https://codeberg.org/gnarz/lpty/archive/version_$pkgver.tar.gz
builddir="$srcdir/lpty"
options="!check"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
done

prepare() {
	default_prepare

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		cd "$builddir-$lver"/src
		make CFLAGS="$CFLAGS -fPIC" \
			LUA_INCDIR="$(pkg-config --variable=includedir lua$lver)" \
			LUA_LIBDIR="$(pkg-config --variable=libdir lua$lver)" \
			LUA=lua$lver
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_pkgname/$pkgver-$_rockver"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	[ "$lver" = 5.1 ] && replaces="$pkgname"

	cd "$builddir-$lver"/src
	make INST_DIR="$subpkgdir/usr" LUAVERSION=$lver install

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="
9a7ed46dabacf663eef03fb711b316acae5266c90c4e5ad1f9aa6857ab7402482c5c38b4ce706bae82622c6c343a56e9bbfcd03ef050983af2ed282bf0195097  lua-pty-1.2.2-1.tar.gz
"
